package com.xzh.rocketmq.service.impl;




import com.mall.common.util.JsonResult;
import com.xzh.mallorderspi.model.Order;
import com.xzh.mallorderspi.model.OrderItem;
import com.xzh.rocketmq.service.OrderFeignService;
import com.xzh.rocketmq.service.ProductFeignService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@RocketMQMessageListener(topic = "mall-orders-check", consumerGroup = "g2")
@Service
public class OrderCheckConsumer implements RocketMQListener<Integer> {
    private OrderFeignService orderFeignService;
    private RedisTemplate<String, Object> redisTemplate;
    private ProductFeignService ProductFeignService;

    @Autowired
    public void setOrderFeignService(OrderFeignService orderFeignService) {
        this.orderFeignService = orderFeignService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }



    @Autowired
    public void setProductFeignService(ProductFeignService ProductFeignService) {
        this.ProductFeignService = ProductFeignService;
    }

    @Override
    public void onMessage(Integer orderId) {
        //从数据库查询订单
        ResponseEntity<JsonResult<Order>> re = orderFeignService.findById(orderId);
        JsonResult<?> jr = re.getBody();
        if (jr != null && jr.isSuccess()) {
            Order order = (Order) jr.getData();
            if (order.getStatus().equals("未支付")) {//超时未支付
                Order forUpdate = new Order();
                forUpdate.setId(order.getId());
                forUpdate.setStatus("已关闭");

                ResponseEntity<JsonResult<?>> re2 = this.orderFeignService.update(forUpdate);
                JsonResult<?> jr2 = re2.getBody();
                if (jr2 != null && jr2.isSuccess()) {
                    //在Redis中进行库存回滚（补偿）
                    List<OrderItem> orderItems = this.findOrderItemsByOrderId(orderId);
                    if (!orderItems.isEmpty()) {
                        Long ProductId = orderItems.getFirst().getGoodId();
                        //redis库存回滚
                        this.redisTemplate.opsForValue().increment("seckill-stock:" + ProductId);

                        //数据库库存回滚
                        this.ProductFeignService.updateQty(Math.toIntExact(ProductId));
                    }
                }
            }
        }
    }

    //查询所有订单项
    private List<OrderItem> findOrderItemsByOrderId(Integer orderId) {
        ResponseEntity<JsonResult<List<OrderItem>>> re = this.orderFeignService.findOrderItemsByOrderId(orderId);
        JsonResult<List<OrderItem>> jr = re.getBody();
        if (jr != null && jr.isSuccess()) {
            return jr.getData();
        } else {
            return List.of();
        }
    }
}
